package com.example.myapplication8;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication8.databinding.ActivityLoginBinding;

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding binding; // View Binding对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化View Binding
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 登录按钮点击事件
        binding.btnLogin.setOnClickListener(v -> login());
    }

    // 登录逻辑
    private void login() {
        // 1. 获取输入的用户名和密码
        String username = binding.etUsername.getText().toString().trim();
        String password = binding.etPassword.getText().toString().trim();

        // 2. 简单验证（实际项目需对接服务器，此处用固定账号演示）
        if (username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        // 模拟正确账号：admin / 123456
        if (!"admin".equals(username) || !"123456".equals(password)) {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
            return;
        }

        // 3. 登录成功：创建User对象，跳转到兴趣选择页
        User user = new User(username);
        Intent intent = new Intent(this, InterestActivity.class);
        intent.putExtra("USER_INFO", user); // 传递用户信息
        startActivity(intent);
        finish(); // 关闭登录页，避免返回时重新显示
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null; // 释放Binding资源
    }
}